from selenium import webdriver
from selenium.common import TimeoutException
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time
from bs4 import BeautifulSoup
import pandas as pd
from sqlalchemy import text
from app_config import get_engine_ts

engine_ts = get_engine_ts()

# 配置Selenium
chrome_options = Options()
chrome_options.add_argument("--headless")  # 无头模式
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
chrome_options.add_argument("--blink-settings=imagesEnabled=false")
chrome_options.add_experimental_option("prefs", {
    "profile.managed_default_content_settings.images": 2,
    "profile.managed_default_content_settings.javascript": 1,  # 保留JS（动态内容可能需要）
})

# 指定Chrome驱动路径
service = Service('./chromedriver.exe')  # 替换为你的chromedriver路径

driver = webdriver.Chrome(service=service, options=chrome_options)
driver.set_page_load_timeout(10)


def get_prince_easymoney(url):
    # page_source = None
    try:
        driver.get(url)
        page_source = driver.page_source
    except TimeoutException:
        print("页面加载超时，尝试继续获取源码")
        page_source = driver.page_source
    soup = BeautifulSoup(page_source, 'html.parser')

    zxj_div = soup.find('div', class_='zxj')
    price_span = zxj_div.find('span').find('span') if zxj_div else None

    # 提取价格文本
    if price_span:
        price = float(price_span.get_text(strip=True))
        return price
    else:
        return 0.00


def get_prince_sina(url):
    driver.get(url)
    page_source = driver.page_source
    soup = BeautifulSoup(page_source, 'html.parser')
    price_span = soup.find('span', class_='real-price')

    if price_span:
        # 获取文本内容并转换为浮点数
        price = float(price_span.text)
        return price
        # print(price)  # 输出: 1311.0
        # # 如果需要保留两位小数
        # print("{:.2f}".format(price))  # 输出: 1311.00
    else:
        return 0.00


def get_prince_baidu(url):
    # page_source = None
    try:
        driver.get(url)
        page_source = driver.page_source
    except TimeoutException:
        print("页面加载超时，尝试继续获取源码")
        page_source = driver.page_source
    soup = BeautifulSoup(page_source, 'html.parser')
    price_span = soup.find('span', class_='b_price')

    if price_span:
        # 获取文本内容并转换为浮点数
        price = float(price_span.text)
        return price
        # print(price)  # 输出: 1311.0
        # # 如果需要保留两位小数
        # print("{:.2f}".format(price))  # 输出: 1311.00
    else:
        return 0.00


def get_data_easymoney(pre, time_, uid):
    pre_time = pre + time_
    url_ = "https://quote.eastmoney.com/qihuo/" + pre_time + ".html"
    print(url_)
    price = get_prince_easymoney(url_)
    # 打开文件（如果不存在会自动创建），以追加模式写入文本
    with open('example.txt', 'a', encoding='utf-8') as file:
        file.write(f"""{pre}_{uid} {time_} {pre_time} {price}\n""")  # 注意手动添加换行符
    print(pre, time_, pre_time, price)


def get_data_sina(pre, time_, uid):
    pre_time = pre + time_

    return get_prince_sina("https://finance.sina.com.cn/futures/quotes/" + pre_time + ".shtml")
    # # 打开文件（如果不存在会自动创建），以追加模式写入文本
    # with open('example.txt', 'a', encoding='utf-8') as file:
    #     file.write(f"""{pre}_{uid} {time_} {pre_time} {price}\n""")  # 注意手动添加换行符
    # print(pre, time_, pre_time, price)


def get_data_baidu(pre, time_, uid):
    pre_time = pre + time_

    price = get_prince_baidu("https://gushitong.baidu.com/futures/ab-" + pre_time)
    # 打开文件（如果不存在会自动创建），以追加模式写入文本
    with open('example.txt', 'a', encoding='utf-8') as file:
        file.write(f"""{pre}_{uid} {time_} {pre_time} {price}\n""")  # 注意手动添加换行符
    print(pre, time_, pre_time, price)


def remove_product(product):
    # 使用 text() 构造带参数的 SQL
    sql = text("DELETE FROM daily_future WHERE product = :param1")
    # 执行 SQL
    with engine_ts.connect() as connection:
        result = connection.execute(sql, {"param1": product})
        connection.commit()
        print(f"删除了 {result.rowcount} 行")
    pass


def fetch_data_code(product, ctime):
    yearMonths = ['2508', '2509', '2510', '2511', '2512', '2601', '2602', '2603', '2604', '2605', '2606', '2607']
    # sina

    # 创建带有指定列名的空DataFrame
    #                        sm2508_ctime sm         sm2508        2508      12.3  sm_ctime      ctime
    df = pd.DataFrame(columns=['id', 'product', 'contract', 'month', 'price', 'group', 'create_time'])
    for month in yearMonths:
        if ['SM', 'SF', 'SA', 'FG', 'SH', 'UR'].__contains__(product):
            price = get_prince_sina(f"""https://finance.sina.com.cn/futures/quotes/{product}{month}.shtml""")
        # elif ['ps', 'rb', 'cu', 'JM', 'ag'].__contains__(product):
        #     price = get_prince_easymoney(f"""https://quote.eastmoney.com/qihuo/{product}{month}.html""")
        else:
            price = get_prince_easymoney(f"""https://quote.eastmoney.com/qihuo/{product}{month}.html""")
        row_data = [f"""{product}{month}-{ctime}""", product, f"""{product}{month}""", month, price, f"""{product}-{ctime}""", ctime]
        df.loc[len(df)] = row_data
        print(row_data)
    df.to_sql('daily_future_history', engine_ts, index=False, if_exists='append', chunksize=5000)
    remove_product(product)
    df.to_sql('daily_future', engine_ts, index=False, if_exists='append', chunksize=5000)
    pass
    # if ['ps', 'rb','cu'].__contains__(product):
    #
    # pass


if __name__ == '__main__':
    from datetime import datetime

    current_time = datetime.now().strftime("%m%d%H%M")
    # fetch_data_code('FG', current_time)  # 玻璃
    # fetch_data_code('SA', current_time)  # 纯碱
    # fetch_data_code('SF', current_time)  # 硅铁
    # fetch_data_code('SM', current_time)  # 锰硅  ---
    # fetch_data_code('SH', current_time)  # 烧碱
    # fetch_data_code('UR', current_time)  # 尿素

    #  广州
    # fetch_data_code('ps', current_time)  # 广州 多晶硅 ----
    fetch_data_code('si', current_time)  # 广州 工业硅
    fetch_data_code('lc', current_time)  # 广州 碳酸锂

    #  大连
    fetch_data_code('rb', current_time)  # 螺纹钢
    fetch_data_code('i', current_time)  # 铁矿石
    fetch_data_code('v', current_time)  # PVC
    fetch_data_code('pp', current_time)  # 聚丙烯
    fetch_data_code('eb', current_time)  # 丙乙烯
    fetch_data_code('JM', current_time)  # 焦煤

    fetch_data_code('cu', current_time)  # 上海 沪铜
    fetch_data_code('ag', current_time)  # 上海 沪银
    fetch_data_code('al', current_time)  # 上海 沪铝
